package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.domain.StrategyReview;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionCommentMapper questionCommentMapper;

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private StrategyReviewMapper strategyReviewMapper;

    @Override
    public PageInfo<Question> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        PageHelper.orderBy("viewCount desc");
        List<Question> list = questionMapper.selectForList(qo);
        for (Question q : list) {
            q.setAnswerCount(answerMapper.getAnswerCountByQuestionId(q.getId()));
            q.setCommentCount(questionCommentMapper.queryForCountById(q.getId()));
            q.setLikeCount(questionMapper.getLikeCountByQuestionId(q.getId()));
        }
        return new PageInfo<Question>(list);
    }

    @Override
    public void saveOrUpdate(Question question) {
        if(question.getId()==null){
            //新增
            question.setCreateTime(new Date());
            if (question.getPlace() == null) {
                question.setPlace("全球");
            }
            question.setViewCount(0);
            question.setFollowCount(0);
            questionMapper.insert(question);
        }else{
            //编辑
            questionMapper.updateByPrimaryKey(question);
        }
    }

    @Override
    public Question getById(Long id) {
        Question question = questionMapper.selectByPrimaryKey(id);
        question.setAnswerCount(answerMapper.getAnswerCountByQuestionId(question.getId()));
        question.setCommentCount(questionCommentMapper.queryForCountById(question.getId()));
        return question;
    }

    // 获取评论数
    @Override
    public int queryForCommentCountById(Long id) {
        return questionCommentMapper.queryForCountById(id);
    }

    // 更新浏览数
    @Override
    public void updateView(Long id) {
        questionMapper.updateView(id);
    }

    @Override
    public void delete(Long id) {
        questionMapper.deleteByPrimaryKey(id);
    }

    // 取消关注
    @Override
    public void delFollow(Long uid, Long qid) {
        questionMapper.delFollow(qid);
        questionMapper.remQuestionFollow(uid, qid);
    }

    // 添加关注
    @Override
    public void addFollow(Long uid, Long qid) {
        questionMapper.addFollow(qid);
        questionMapper.insQuestionFollow(uid, qid);
    }

    // 获取问题是否被某用户关注
    @Override
    public Question getFollowQuestionById(Long uid, Long qid) {
        return questionMapper.getFollowQuestionById(uid, qid);
    }

}
